package yxy.game.pm2.module.shop.handle;

import org.joda.time.DateTime;

import org.joda.time.LocalDate;
import yxy.cherry.data.bean.breakshop;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.message.MerchantMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.shop.Goods;
import yxy.game.pm2.module.shop.GoodsLimitType;
import yxy.game.pm2.module.shop.MerchantModule;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

public class MerchantHandle extends ShopHandle<breakshop> {

    public void packResponse(String playerCode, MerchantMessage.MerchantResponse.Builder response) {
        MerchantModule module = getModule(playerCode);
        response.addAllGoods(goods(playerCode));
        if (module.MerchantRefreshCost() != null) {
            response.setResetCost(new CurrencyHandle().packPoint(module.MerchantRefreshCost()));
        }
        response.setResetFree(module.getFreeTimes());//免费刷新剩余次数
        response.setResetTimes(module.getResetTimes());//付费刷新次数
        checkTip(module);
    }

    /**
     * 手动刷新
     */
    public Result refresh(String playerCode, MerchantMessage.MerchantUpdateResponse.Builder response) {
        MerchantModule module = MerchantModule.get(playerCode);
        if (!module.canRefresh()) {
            return Result.ShopCannotRefresh;
        }
        //免费次数还有就消耗免费次数，否则走付费刷新
        if (module.getFreeTimes() > 0) {
            //剩余免费次数-1
            module.setFreeTimes(module.getFreeTimes() - 1);
            module._refresh();
            packRefresh(playerCode, module, response);
            return Result.Success;
        }

        //剩余付费次数-1
        Currency cost = module.MerchantRefreshCost();// 刷新的消费
        module.setResetTimes(module.getResetTimes() - 1);

        try {
            new CurrencyHandle().cost(playerCode, CCReason.商店, cost);
        } catch (CurrencyNotEnoughException e) {
            return new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
        }
        module._refresh();
        packRefresh(playerCode, module, response);
        checkTip(module);
        return Result.Success;
    }

    private void packRefresh(String playerCode, MerchantModule module, MerchantMessage.MerchantUpdateResponse.Builder response) {
        response.addAllGoods(new MerchantHandle().goods(playerCode));
        if (module.MerchantRefreshCost() != null) {
            response.setResetCost(new CurrencyHandle().packPoint(module.MerchantRefreshCost()));
        }
        response.setResetFree(module.getFreeTimes());
        response.setResetTimes(module.getResetTimes());
    }

    public Result buy(String playerCode, int id, int num, MerchantMessage.MerchantBuyResponse.Builder response) {
        BuyResponse buyResponse = new BuyResponse();
        Result result = buy(playerCode, id, num, buyResponse);
        if (result.equals(Result.Success)) {
            response.setPurchase(buyResponse.getPurchase());
            response.setGoods(buyResponse.getGoods());
            TaskHandle.handle(playerCode, TaskType.统计型_市集_购买_次数, 1);
        }
        return result;
    }

    @Override
    public MerchantModule getModule(String playerCode) {
        MerchantModule module = MerchantModule.get(playerCode);
        DateTime now = DateTime.now();
        boolean needSet = false;
        for (Goods goods : module.getGoods()) {
            needSet |= update(goods, now);
        }
        if (needSet) {
            module.set();
        }

        //如果是下一天，刷新免费和付费的刷新次数
        if (!new LocalDate(now).equals(new LocalDate(module.getRefreshTime()))) {
            module.setRefreshTime(now);
            module.setFreeTimes(module.RefreshFreeUpper());
            module.setResetTimes(module.RefreshUpper());
            module.set();
        }

        checkTip(module);
        return module;
    }

    /**
     * 检查红点
     */
    private void checkTip(MerchantModule module) {
        new TipHandle().updateTip(module.getPlayerCode(), TipType.商店_市集_免费刷新, module.getFreeTimes() > 0);
    }

    @Override
    public GoodsLimitType limitType(breakshop data) {
        return GoodsLimitType.forNumber(data.getLimittype());
    }

    @Override
    public int limit(breakshop data) {
        return data.getLimit();
    }

    @Override
    public int rebate(breakshop data) {
        return data.getReduce();
    }

    @Override
    public Currency cost(breakshop data, int num) {
        return Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() * num);
    }

    @Override
    public Currency purchase(breakshop data, int num) {
        return Currency.create(data.getShoptype(), data.getShopid(), data.getShopnum() * num);
    }

    @Override
    public Class<breakshop> clazz() {
        return breakshop.class;
    }
}
